package com.example.my.agiftthat.ui.list;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.bumptech.glide.Glide;
import com.example.my.agiftthat.R;
import com.example.my.agiftthat.helper.OkHttpClientHelper;
import com.example.my.agiftthat.helper.SpacesItemDecoration;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/12/3.
 */

public class CommentFragment extends Fragment {
    @Bind(R.id.comment_titleImage)
    ImageView commentTitleImage;
    @Bind(R.id.comment_recyclerView)
    RecyclerView commentRecyclerView;
    private Context mContext = null;
    private FragmentAdapter adapter;
    private List<UserBean.DataBean.ItemsBean> itemsBeanList = null;
    private Handler handler = new Handler() {


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

                    UserBean userBean = (UserBean) msg.obj;
                    if(!(userBean.getData().getCover_image() == null)) {
                        Glide.with(mContext).load(userBean.getData().getCover_image()).into(commentTitleImage);
                    }
                    itemsBeanList = userBean.getData().getItems();
                    adapter = new FragmentAdapter(mContext, itemsBeanList);
                    commentRecyclerView.setAdapter(adapter);
                    commentPullTo.onRefreshComplete();
                    break;
            }
        }
    };
    private PullToRefreshScrollView commentPullTo;
    private String index;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        itemsBeanList = new ArrayList<>();
        View view = inflater.inflate(R.layout.fragment_comment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        commentPullTo = (PullToRefreshScrollView) view.findViewById(R.id.comment_pullTo);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        commentRecyclerView.setHasFixedSize(true);
//        commentRecyclerView.addItemDecoration(new SpacesItemDecoration(5));
        GridLayoutManager manager = new GridLayoutManager(mContext, 2);
        commentRecyclerView.setLayoutManager(manager);
        //下载获得的url数据并且解析
        Bundle bundle = getArguments();
        index = bundle.getString("index");
        dataJson(index);
        //下拉刷新
        commentPullTo.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
//                Log.i("aaaaa","走了1");
                if(!(adapter == null)) {
                    adapter.reloadRecyclerView(itemsBeanList, true);
                    dataJson(index);
                }
                commentPullTo.onRefreshComplete();
            }
        });
    }

    private void dataJson(final String path) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String run = OkHttpClientHelper.getStringFromURL(mContext, path, 1);
//                    Log.i("aaaaa","走了2"+run);
                    Gson gson = new Gson();
                    UserBean userBean = gson.fromJson(run, UserBean.class);
                    Message msg = handler.obtainMessage();
                    msg.what = 1;
                    msg.obj = userBean;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
